package com.itawei.TCPpractise.num3;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;

/**
 * 客户端：将本地文件上传到服务器。接收服务器的反馈
 * 服务器：接收客户端上传的文件，上传完毕之后给出反馈
 */
public class Server {
    public static void main(String[] args) throws IOException {
        //1.创建Socket对象
        ServerSocket ss = new ServerSocket(10086);
        //2.等待客户端连接 并返回socket对象
        Socket socket = ss.accept();
        //3.创建缓冲流 输入流对象
        BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
        String str = UUID.randomUUID().toString().replace("-", "");
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\Program Files\\Java\\code\\javaseadvrepro\\bbb\\"+str+".jpg"));
        byte[] bytes = new byte[1024];
        int len;
        while ((len = bis.read(bytes)) != -1) {
            bos.write(bytes, 0, len);
        }

        //4.回写
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bw.write("上传成功");
        bw.newLine();
        bw.flush();

        //5.释放资源
        socket.close();
        ss.close();
    }
}
